package com.reisdelay.config;

import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;

@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    public RedisCacheManager cacheManager(RedisConnectionFactory factory) {
        return RedisCacheManager.create(factory);
    }

    @Bean
    public <T> RedisTemplate<T, T> redisTemplate(RedisConnectionFactory factory) {
        RedisTemplate<T, T> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        //这里选用Json格式的对象序列化器
        Jackson2JsonRedisSerializer<T> serializer = new Jackson2JsonRedisSerializer<T>((Class<T>) Object.class);
        redisTemplate.setDefaultSerializer(serializer);//设置默认的序列化器为Json序列化器
        return redisTemplate;
    }
}